package com.hjk.waibao.mj.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

import java.lang.reflect.Parameter;

/**
 * @Description 验证页码之类的
 * @Author hjk
 * @Date 2020/8/20
 **/
public class PageFixInterceptor implements Interceptor {
    @Override
    public void intercept(Invocation invocation) {
        for (int index = 0; index < invocation.getMethod().getParameters().length; index++) {
            Parameter parameter = invocation.getMethod().getParameters()[index];
            String name = parameter.getName();
            if ("pageNumber".equals(name)) {
                Object value = invocation.getArg(index);
                if (value == null || (Integer) value < 1) {
                    invocation.setArg(index, 1);
                }
            }
            if ("pageSize".equals(name)) {
                Object value = invocation.getArg(index);
                if (value == null || (Integer) value < 1 || (Integer) value > 50) {
                    invocation.setArg(index, 10);
                }
            }
        }
        invocation.invoke();

    }
}
